package com.hd.trans.ui.adapter

import android.annotation.SuppressLint
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.core.view.isVisible
import com.airbnb.lottie.LottieAnimationView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hd.trans.R
import com.hd.trans.TranslationInterior
import com.hd.trans.TranslationUI
import com.hd.trans.common.Constants
import com.hd.trans.db.DataBaseMgr
import com.hd.trans.db.bean.TranslateRecord
import com.hd.trans.dialog.VoiceTransRecordMenuPop
import com.hd.trans.framework.click.XClickUtil
import com.hd.trans.utils.ToastUtils
import com.hd.trans.widgets.VoicePlayView


/**
 * Author:  yang
 * Date:    2020/01/07 15:58
 * Desc:    文本翻译记录适配器
 */
class RecordVoiceAdapter3 :
    BaseMultiItemQuickAdapter<TranslateRecord, BaseViewHolder>() {

    interface VoiceClickListener {
        fun onMenuEditClick(position: Int)
        fun onMenuCopyClick(position: Int)
        fun onMenuShareClick(position: Int)
        fun onMenuDeleteClick(position: Int)
        fun onMenuDeleteAllClick(position: Int)
        fun onVoicePlayClick(position: Int,voicePlayView:VoicePlayView,isTrans:Boolean) //播放
        fun onRetryClick(position: Int) //重新翻译
        fun onMenuCollectClick(position: Int)
    }
    var autoPlayData:TranslateRecord? = null
    private var mVoiceClickListener: VoiceClickListener? = null
    private var selectedDataPosition = 0


    fun updateChange(record: TranslateRecord?) {
        if(record == null){
            return
        }
        val position = indexOf(record)
        if(position >= 0 && position < data.size){
            set(position, record)
        }
    }

    fun indexOf(record: TranslateRecord?): Int {
        return data.indexOf(record)
    }

    fun set(location: Int, record: TranslateRecord) {
        if (location < 0 || location >= data.size) {
            return
        }
        data[location] = record
        notifyItemChanged(location)
    }


    //设置监听接口
    fun setVoiceClickListener(voiceClickListener: VoiceClickListener?) {
        this.mVoiceClickListener = voiceClickListener
    }

    //布局转换
    @SuppressLint("SetTextI18n")
    override fun convert(holder: BaseViewHolder, item: TranslateRecord) {
        val text1 = holder.getView<TextView>(R.id.text_1)
        val text2 = holder.getView<TextView>(R.id.text_2)
        //条目复用初始化清空内容
        text1.text = item.contentOrig ?: ""
        text2.text = ""
        val voicePlayView2 = holder.getView<VoicePlayView>(R.id.view_voice_2)
        if(autoPlayData == item){
            autoPlayData = null
            voicePlayView2.performClick()
        }
        val lottieLoading = holder.getView<LottieAnimationView>(R.id.lottie_loading)
        when (holder.itemViewType) {
            0 -> {
                text2.setTextColor(TranslationInterior.getThemeColor())
                //lottieLoading.setAnimation(R.raw.loading_point)
            }
            1 -> {
                //lottieLoading.setAnimation(R.raw.loading_point_2)
            }
        }

        when (item.translateState) {
            Constants.VOICE_RECORD_NORMAL -> {
                text2.text = item.contentTranslate ?: ""
                holder.setGone(R.id.view_voice_2, false)
                lottieLoading.cancelAnimation()
                lottieLoading.isVisible = false
            }

            Constants.VOICE_RECORD_TRANSLATING -> {
                holder.setGone(R.id.view_voice_2, true)
                lottieLoading.isVisible = true
                lottieLoading.post {
                    lottieLoading.playAnimation()
                }
            }
            else -> {
                lottieLoading.cancelAnimation()
                lottieLoading.isVisible = false
            }
        }
    }

    private val mMenuPop by lazy {
        VoiceTransRecordMenuPop(context).apply {
            onItemClick = {
                when(it){
                    "delete" -> {
                        mVoiceClickListener?.onMenuDeleteClick(selectedDataPosition)
                    }
                    "edit" -> {
                        mVoiceClickListener?.onMenuEditClick(selectedDataPosition)
                    }
                    "copy" -> {
                        mVoiceClickListener?.onMenuCopyClick(selectedDataPosition)
                    }
                    "share" -> {
                        mVoiceClickListener?.onMenuShareClick(selectedDataPosition)
                    }
                    "collect" -> {
                        val data = getItem(selectedDataPosition)
                        data.isCollected = !data.isCollected
                        DataBaseMgr.getInstance().updateTranslateRecord(data)
                        if(data.isCollected){
                            ToastUtils.showNormal(R.string.collection_success)
                        }else{
                            ToastUtils.showNormal(R.string.collection_cancel)
                        }
                        this.data = data
                        mVoiceClickListener?.onMenuCollectClick(selectedDataPosition)
                    }
                    "enlarge" -> {
                        val data = getItem(selectedDataPosition)
                        TranslationUI.openFullScreenTransActivity(context, data.contentTranslate)
                    }
                }
            }
        }
    }

    init {
        addItemType(0, R.layout.trans_voice_record_left_layout) //正
        addItemType(1, R.layout.trans_voice_record_right_layout) //逆

        // 先注册需要点击的子控件id（注意，请不要写在convert方法里）
        addChildClickViewIds(R.id.ll_trans_layout, R.id.btn_more, R.id.view_voice_1, R.id.view_voice_2)
        setOnItemChildClickListener(OnItemChildClickListener { _, view, position ->
            if (XClickUtil.isFastDoubleClick(view, XClickUtil.INTERVAL_MILLIS.toLong())) {
                return@OnItemChildClickListener
            }
            Log.w("position", "$position---")
            val id = view.id
            if (id == R.id.view_voice_1) {
                if (view is VoicePlayView) {
                    mVoiceClickListener?.onVoicePlayClick(position, view,false)
                }
            } else if (id == R.id.view_voice_2) {
                if (view is VoicePlayView) {
                    mVoiceClickListener?.onVoicePlayClick(position, view,true)
                }
            } else if (id == R.id.btn_more) {
                showPop(view, position)
            } else if (id == R.id.ll_trans_layout) {
                if (TextUtils.isEmpty(data[position].contentTranslate)) {
                        mVoiceClickListener?.onRetryClick(position)
                } else {
                    showPop(view, position)
                }
            }
        })
    }

    fun size():Int{
        return data.size
    }

    private fun showPop(view: View, posstion: Int) {
        selectedDataPosition = posstion
        mMenuPop.data = data[posstion]
        mMenuPop.showAsDropDown(view)
    }
}
